package com.dcy.security.auth.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dcy.security.auth.mapper.SysUserInfoMapper;
import com.dcy.security.auth.model.SysUserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author dcy
 * @Date: 2019/3/19 16:31
 * @Description:
 */
@Service
public class UserServiceDetailImpl implements UserDetailsService {

    @Autowired
    private SysUserInfoMapper sysUserInfoMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //查询用户信息
        SysUserInfo sysUserInfo = sysUserInfoMapper.selectOne(new QueryWrapper<SysUserInfo>().lambda().eq(SysUserInfo::getUsername, username));
        if (sysUserInfo != null) {
            // 获取菜单列表
            sysUserInfo.setPermissionSet(sysUserInfoMapper.getPermissionListByUserId(sysUserInfo.getUserId()));
            // 获取角色列表
            sysUserInfo.setGrantedAuthoritySet(sysUserInfoMapper.getAuthRoleListByUserId(sysUserInfo.getUserId()));
            return sysUserInfo;
        }
        return null;
    }
}
